home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1067 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  58 lines

  1. Path: news.tcd.net!news
  2. From: Steve <slavis@PW1.PrairieWeb.Com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Q: Please help me to write to .dat file!
  5. Date: 9 Jan 1996 03:51:39 GMT
  6. Organization: The Computer Den, Inc. Evanston WY
  7. Message-ID: <4csoob$kj@news.tcd.net>
  8. NNTP-Posting-Host: pw1.prairieweb.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  13.  
  14. OK, I just about have this code whipped EXCEPT for one problem.
  15. Here's what I'm trying to do:
  16. I'm trying to write binary information to a .dat file. This code is not
  17. designed to READ from the file, ONLY to WRITE to the .dat file.
  18. I am writing structures of various questions to the file.
  19. It first asks to "Enter question number" , I enter: 1
  20. It then asks "Enter answer", I would *like* to enter: B  *BUT* the program
  21. doesn't halt to allow me to enter the answer B
  22. It instead skips to "Enter question", I enter: Why is the sky blue?
  23. Thquestion is successfully written to file.
  24. Why can't I enter my answer B????? Can one only enter info into the input
  25. stream only once or something like that?????
  26. Anyway, here's the code I've struggled with.
  27. #include <iostream.h>
  28. #include <fstream.h>
  29. #include <stdlib.h>
  30. main()
  31. {
  32. struct cpaQuestion {
  33.                 int questionNum;
  34.                 char question[];
  35.                 char answer[];
  36.                 };
  37.         ofstream outQuestion("cpa.dat", ios::ate);
  38.         if(!outQuestion) {
  39.                 cerr << "File isn't going to open." << endl;
  40.                 exit(1);
  41.         }
  42. cout << "Enter the question number:" << endl;
  43. cpaQuestion audit;
  44. cin >> audit.questionNum;
  45. if(audit.questionNum > 0 && audit.questionNum <= 100) {
  46.         cout << "Enter answer:" <<endl;
  47.         cin.getline(audit.answer, 3);
  48.         }
  49. if(audit.questionNum > 0 && audit.questionNum <= 100) {
  50.         cout << "Enter question:" <<endl;
  51.         cin.getline(audit.question, 200);
  52.         }
  53.         outQuestion.seekp((audit.questionNum - 1) *sizeof(audit));
  54.         outQuestion.write((char *)&audit, sizeof(audit));
  55.         return 0;
  56. }
  57.  
  58.